package com.example.shiwu.service;

import android.content.Context;

import com.example.shiwu.db.dao.BoxFiveDao;
import com.example.shiwu.db.dao.base.BaseDaoFactory;
import com.example.shiwu.db.model.BoxFive;
import com.example.shiwu.db.model.BoxOne;
import com.example.shiwu.manager.DatabaseHelper;
import com.example.shiwu.util.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class BoxFiveService {

    private DatabaseHelper dbHelper;
    private BoxFiveDao<BoxFive> BoxFiveDao;
    private ArrayList<BoxFive> listBoxFive = null;

    private static final int PAGE_SIZE = 10;
    private static final int TOTAL_PAGE = 0;

    public BoxFiveService() {
    }

    public BoxFiveService(Context context) {
        dbHelper = new DatabaseHelper(context);
    }


    /**获取单个数据
     * @param BoxFive
     * @return
     */
    public List<BoxFive> getBoxFive(BoxFive BoxFive) {
        BoxFiveDao = BaseDaoFactory.getInstance().createBaseDao(BoxFiveDao.class, BoxFive.class);
        listBoxFive = new ArrayList<>();
        listBoxFive.addAll(BoxFiveDao.query(BoxFive));
        return listBoxFive;
    }

    /**分页获取数据
     * @param BoxFive
     * @return
     */
    public List<BoxFive> getBoxFiveList(BoxFive BoxFive,Integer startIndex,Integer limit) {
        BoxFiveDao = BaseDaoFactory.getInstance().createBaseDao(BoxFiveDao.class, BoxFive.class);
        listBoxFive = new ArrayList<>();
        listBoxFive.addAll(BoxFiveDao.query(BoxFive,null,"create_time desc",null,startIndex,limit));
        return listBoxFive;
    }


    public boolean save(BoxFive BoxFive) {
        BoxFiveDao = BaseDaoFactory.getInstance().createBaseDao(BoxFiveDao.class, BoxFive.class);
        long size = BoxFiveDao.insert(BoxFive);
        if(size >0 ){
            return true;
        }
        return false;
    }
    public boolean saveOrUpdata(BoxFive BoxFive) {
        BoxFiveDao = BaseDaoFactory.getInstance().createBaseDao(BoxFiveDao.class, BoxFive.class);
        BoxFive boxFive = new BoxFive();
        boxFive.setItemId(BoxFive.getItemId());
        List<BoxFive> lis =BoxFiveDao.query(boxFive);
        Boolean bl = true;
        if(lis.size()>0){
            BoxFive.setUpdateTime(DateUtils.parseDate());
            long size = BoxFiveDao.update(BoxFive,new BoxFive());
            if(size >0 ){
                return bl;
            }
            bl = false;
        }else{
            BoxFive.setCreateTime(DateUtils.parseDate());
            long size = BoxFiveDao.insert(BoxFive);
            if(size >0 ){
                return bl;
            }
            bl = false;
        }

        return bl;
    }

    public boolean updade(BoxFive BoxFive,BoxFive where) {
        long size = BoxFiveDao.update(BoxFive,where);
        if(size >0 ){
            return true;
        }
        return false;
    }

    public Boolean delete(BoxFive boxFive) {
        int size = BoxFiveDao.delete(boxFive);
        if(size >0 ){
            return true;
        }
        return false;
    }
}
